package com.bw.lib_base.mvvm

import android.app.DatePickerDialog
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.widget.DatePicker
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.ViewModelProvider
import java.lang.reflect.ParameterizedType
import java.util.Calendar

abstract class BaseActivity<VDB:ViewDataBinding,VM:BaseViewModel>:AppCompatActivity() {

    //获取当前类的泛型参数列表
    private val type by lazy { (javaClass.genericSuperclass as ParameterizedType).actualTypeArguments }

    protected val vm:VM by lazy {
        //读取第二个参数
        val clazz = type[1] as Class<VM>
        //根据传入的VideModel获取对应的VideModel对象
        ViewModelProvider(this)[clazz]
    }

    protected val binding by lazy {
        val clazz = type[0] as Class<VDB>
        val method = clazz.getMethod("inflate", LayoutInflater::class.java)
        method.invoke(null,layoutInflater) as VDB
    }

    fun onError(t:Throwable){
        Log.i("===",t.message.toString())
        t.printStackTrace()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)
        vm.onFailed.observe(this,::onError)
        initView()
        initData()
    }
    abstract fun initView()

    abstract fun initData()


}